package com.chunuo.blogs.controller;

import com.chunuo.blogs.entity.Tag;
import com.chunuo.blogs.service.TagService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签接口
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-17 15:43
 */
@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 获取所有标签
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/tag")
    public PageInfo findAll(@RequestParam("page")Integer page, @RequestParam("size")Integer size){
        List<Tag> tags = tagService.findAll(page,size);
        return new PageInfo(tags);
    }

    /**
     * 根据id找到标签
     * @param id
     * @return
     */
    @GetMapping("tag/{id}")
    public Tag findById(@PathVariable("id")Integer id){
        Tag tag = tagService.findById(id);
        return tag;

    }

    /**
     * 添加标签
     * @param tag
     * @return
     */
    @PostMapping("/tag")
    public Integer add(@RequestBody Tag tag){
        tagService.add(tag);
        return 1;
    }

    /**
     * 更新标签
     * @param tag
     * @return
     */
    @PutMapping("/tag")
    public  Integer update(@RequestBody Tag tag){
        tagService.update(tag);
        return 1;
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @DeleteMapping("/tag/{id}")
    public Integer delete(@PathVariable("id") Integer id){
        tagService.delete(id);
        return 1;
    }

}
